package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.config.result.ResultCode;
import com.future.hair.server.entity.Manager;
import com.future.hair.server.mapper.ManagerMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("//hair/login")
@Api(tags = "login业务接口")
public class LoginController {

    @Autowired
    private ManagerMapper mapper;

    @ApiOperation(value = "登录 login")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    public CommonResult<Manager> login(@RequestBody Manager data) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<Manager>();
        wrapper.eq("username", data.getUsername());
        Manager manager1 = mapper.selectOne(wrapper);

        QueryWrapper<Manager> wrapper1 = new QueryWrapper<Manager>();
        wrapper1.eq("mobile", data.getMobile());
        Manager manager2 = mapper.selectOne(wrapper1);
        Boolean bool1 = (null == manager1 || !manager1.getPassword().equals(data.getPassword()));
        Boolean bool2 = (null == manager2 || !manager2.getPassword().equals(data.getPassword()));
        if (bool1 && bool2) {
            return CommonResult.failed(ResultCode.USERNAME_OR_PASSWORD_ERROR);
        }
        Manager manager = new Manager();
        if (manager1 != null) {
            manager = manager1;
        } else {
            manager = manager2;
        }
        if (null==manager.getUsername()){
            manager.setUsername("");
        }
        if (null==manager.getMobile()){
            manager.setMobile("");
        }
        if (null==manager.getOpenId()){
            manager.setOpenId(0);
        }
        if (null==manager.getMallId()){
            manager.setMallId(0);
        }
        String jwtToken = JwtHelper.generateToken(manager.getUsername(),manager.getMobile(), manager.getId(), manager.getOpenId(),manager.getMallId());
        return CommonResult.data(manager, jwtToken);
    }

    @ApiOperation(value = "退出登录 logout")
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public CommonResult<String> logout(@RequestHeader String token) {
//        boolean isLogout = redisTemplate.delete(token);
//        if (isLogout){
//            return CommonResult.success("退出成功");
//        }else{
//            return CommonResult.failed("");
//        }
        return CommonResult.success("退出成功");
    }


    @ApiOperation(value = "hello")
    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public CommonResult<String> hello(@RequestBody PageModel data) {
        return CommonResult.data("hello hair" + data.getSize() + data.getCurrent());
    }
}



